package com.binvshe.binvshe.app.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.view.CommentDialog;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * FileName: CommentActivity
 *
 * @CretedBy : zofnk
 * @CreateDate: 2016/9/14 14:44
 * @E-mail: zofnk@vip.qq.com
 * @RevisionsDate：
 * @ModifyNotes：
 */
public class CommentActivity extends BaseActivity {

    public String article = "article";
    public static final String KEY_TARGET_ID = "key_target_id";
    public static final String KEY_FOUSE = "key_fouse";

    @InjectExtra(KEY_TARGET_ID)
    int mTargetId;

    @InjectExtra(KEY_FOUSE)
    boolean mIsFouse;

    @Bind(R.id.mTvComment)
    TextView mTvComment;

    /**
     * @param context
     * @param targetId
     * @param isFouse  是否请求焦点
     */
    public static void start(Context context, int targetId, boolean isFouse) {
        Intent starter = new Intent(context, CommentActivity.class);
        starter.putExtra(KEY_TARGET_ID, targetId);
        starter.putExtra(KEY_FOUSE, isFouse);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("所有评论");
        enableBackButton();
        if (mIsFouse) {
            showCommentDialog();
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Dart.inject(this);
        View view = inflater.inflate(R.layout.act_comment, null);
        FragmentUtils.addFirstFragment(getSupportFragmentManager(), R.id.mCommentContainer, CommentFragment.newInstance(mTargetId,article));
        return view;
    }

    @OnClick(R.id.mTvComment)
    public void onClick() {
        showCommentDialog();
    }

    private void showCommentDialog() {
        CommentDialog commentDialog = CommentDialog.newInstance("评论", "写评论", 4, mIsFouse);
        commentDialog.show(getSupportFragmentManager(), "tag");
        commentDialog.setOnClickable(new CommentDialog.CommentClickable() {

            @Override
            public void btnEnter(String comment) {
                MyHttpRequest request = new MyHttpRequest(CommentActivity.this);
                String url = String.format(UrlCenter.COMMENT, article, mTargetId);
                JSONObject jobj = new JSONObject();
                try {
                    jobj.put(ParamKey.CONTENT, comment);
                    request.post(url, jobj, new DataRequestListener<String>(String.class) {
                        @Override
                        public void onStart(Context context, String url) {
                            enableProgress(false);
                            super.onStart(context, url);
                        }

                        @Override
                        public void onSuccess(String str) {
                            super.onSuccess(str);
                            EventBus.getDefault().post(new ClassEvent.RefreshEvent());
                        }

                        @Override
                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                            super.onFailure(t, errorNo, strMsg);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
